package com.star.modules.sys.service;

import com.jeesite.common.entity.Page;
import com.jeesite.common.mapper.BeanMapper;
import com.jeesite.modules.sys.utils.DictUtils;
import com.star.modules.sys.base.BizDataResult;
import com.star.modules.sys.dao.ShHouseDao;
import com.star.modules.sys.dao.ShInfoDao;
import com.star.modules.sys.dao.ShPicDao;
import com.star.modules.sys.entity.ShHouse;
import com.star.modules.sys.entity.ShInfo;
import com.star.modules.sys.entity.ShPic;
import com.star.modules.sys.enums.DictEnum;
import com.star.modules.sys.enums.ResultEnum;
import com.star.modules.sys.excetion.ApiException;
import com.star.modules.sys.vo.IndexVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 首页服务类
 */
@Service
@Transactional(readOnly = true)
public class IndexService {
    //查看图片查前5张
    public final static int PIC_PAGE_NO = 1;
    public final static int PIC_PAGE_SIZE = 5;
    //房源展示数目
    public final static int HOUSE_PAGE_SIZE = 8;
    //咨询与新闻动态数目
    public final static int MATION_NEWS_SIZE = 5;
    //查看房源前8
    //关于我们ID
    public final static String ABOUT_AU_ID = "1";
    //资询百科类型
    public final static int INFO_MATION_TYPE = 2;
    public final static int NEWS_TYPE = 3;

    //默认结果
    public final static String DEFAULT_LABEL = "无";


    @Autowired
    private ShPicDao shPicDao;

    @Autowired
    private ShInfoDao shInfoDao;

    @Autowired
    private ShHouseDao shHouseDao;
    /**
     * 首页业务层
     *
     * @return
     */
    public BizDataResult getIndexInfo() {
        IndexVo indexVo = new IndexVo();
        try {
            //轮播图
            ShPic shPic = new ShPic();
            shPic.setStatus(ShPic.STATUS_NORMAL);
            shPic.setPage(new Page<ShPic>(PIC_PAGE_NO, PIC_PAGE_SIZE));
            indexVo.setPics(BeanMapper.mapList(shPicDao.findList(shPic), IndexVo.PicVo.class));
            //关于我们
            ShInfo infoQuery = new ShInfo();
            infoQuery.setId(ABOUT_AU_ID);
            indexVo.setAboutAs(BeanMapper.map(shInfoDao.get(infoQuery),IndexVo.InfoVo.class));
            //房源查询
            ShHouse shHouse = new ShHouse();
            shHouse.setStatus(ShHouse.STATUS_NORMAL);
            shHouse.setPage(new Page<ShHouse>(PIC_PAGE_NO,HOUSE_PAGE_SIZE));
            List<IndexVo.HouseVo> houseVos = BeanMapper.mapList(shHouseDao.findList(shHouse),IndexVo.HouseVo.class);
            //保证为偶数
            if( houseVos.size()%2 != 0){
                houseVos = houseVos.subList(0,houseVos.size()-1);
            }
            /*for(IndexVo.HouseVo vo : houseVos){
                vo.setHouseTypeStr(DictUtils.getDictLabel(DictEnum.HOUSE_TYPE.getCode(),vo.getHouseType()+"",DEFAULT_LABEL));
            }*/
            indexVo.setHouses(houseVos);
            //资讯百科
            infoQuery = new ShInfo();
            infoQuery.setType(INFO_MATION_TYPE);
            infoQuery.setStatus(ShInfo.STATUS_NORMAL);
            infoQuery.setPage(new Page<ShInfo>(PIC_PAGE_NO,MATION_NEWS_SIZE));
            indexVo.setInfoMations(BeanMapper.mapList(shInfoDao.findList(infoQuery),IndexVo.InfoVo.class));
            //新闻动态
            infoQuery.setType(NEWS_TYPE);
            indexVo.setNews(BeanMapper.mapList(shInfoDao.findList(infoQuery),IndexVo.InfoVo.class));
        } catch (Exception e) {
            throw new ApiException(e);
        }
        return BizDataResult.createResult(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), indexVo);
    }
}
